package organizer.ui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import organizer.main.Day;
import organizer.main.Organizer;

public class UI extends JFrame {
	// private static UI instance = null;

	private GridBagConstraints gbc = null;

	private JPanel panelPanel = null;

	private Organizer organizer = null;

	// public static UI getInstance() {
	// if (instance == null) {
	// instance = new UI();
	// }
	// return instance;
	// }

	public Organizer getOrganizer() {
		return organizer;
	}

	public UI(Organizer organizer) {
		this.organizer = organizer;
		this.gbc = new GridBagConstraints();

		init();
		addComponents();

		updatePanels();

		display();
	}

	// public static UI createInstance(Vector<Day> days) {
	// return instance = new UI(days);
	// }

	private static GridBagConstraints getNormalGBC() {
		return new GridBagConstraints(0, 0, 1, 1, 0, 0,
				GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
						0, 0, 0, 0), 0, 0);
	}

	private GridBagConstraints getNormalGBC(GridBagConstraints oldPosition) {
		return new GridBagConstraints(oldPosition.gridx, oldPosition.gridy, 1,
				1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
				new Insets(0, 0, 0, 0), 0, 0);
	}

	private GridBagConstraints getNormalGBCWithXY(int gridx, int gridy) {
		return new GridBagConstraints(gridx, gridy, 1, 1, 0, 0,
				GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
						0, 0, 0, 0), 0, 0);
	}

	private void init() {
		this.setTitle("Organisierer!");

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new GridBagLayout());

		// this.setPreferredSize(new Dimension(800, 600));
	}

	private void addComponents() {
		addMenu();

		addWeekOfYearPanel();
		addPanels();
	}

	private void addMenu() {
		// Create the menu bar.
		JMenuBar menuBar = new JMenuBar();

		// Build the first menu.
		JMenu menu = new JMenu("A Menu");
		menu.setMnemonic(KeyEvent.VK_A);
		menu.getAccessibleContext().setAccessibleDescription(
				"The only menu in this program that has menu items");
		menuBar.add(menu);

		// a group of JMenuItems
		JMenuItem menuItem = new JMenuItem("A text-only menu item",
				KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1,
				ActionEvent.ALT_MASK));
		menuItem.getAccessibleContext().setAccessibleDescription(
				"This doesn't really do anything");
		menu.add(menuItem);

		this.setJMenuBar(menuBar);
	}

	private void addWeekOfYearPanel() {
		WeekOfYearPanel woyPanel = new WeekOfYearPanel(this);
		gbc = getNormalGBC();
		gbc.gridwidth = 7;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		add(woyPanel, gbc);
	}

	private void addPanels() {
		gbc.gridy++;
		panelPanel = new JPanel();
		panelPanel.setLayout(new GridBagLayout());
		panelPanel.setBackground(Color.cyan);
		gbc = getNormalGBC(gbc);
		System.out.println("UI gbc.gridy: " + gbc.gridy);
		add(panelPanel, gbc);
	}

	public void updatePanels() {
		Vector<Day> days = organizer.getDays();
		panelPanel.removeAll();
		panelPanel.validate();

		System.out.println("Creating " + days.size() + " panels...");
		GridBagConstraints gbcPanel = getNormalGBC();
		GregorianCalendar gcal = new GregorianCalendar();
		for (int i = 0; i < days.size(); i++) {
			DayPanel dayPanel = new DayPanel(days.get(i));

			panelPanel.add(dayPanel, gbcPanel);
			gcal.setTime(dayPanel.getDay().getDate());
			gbcPanel.gridx++;
			if (gcal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
				gbcPanel.gridy++;
				gbcPanel.gridx = 0;
				System.out.println("gbcPanel.gridy: " + gbcPanel.gridy);
			}
		}
	}

	public void display() {
		// this.add(this);
		this.pack();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
}
